<?php
/**
  *@function 得到所有的分类信息
  *
  *@date 2013-05-29 20:17
  *
  */
class M_atlas extends CI_Model {
	
	private $table_name = 'zy_atlas';
	private $user_id;

    function __construct()
    {
        parent::__construct();
		$this->load->database();
		$this->user_id = $this->session->userdata('user_id');
    }
	
	/**
	 * 新增图谱课程
	 */
	public function insertCourse($data,$type,$atl_id)
	{
		if($type=='3')
		{
			$course = array('cour_name'=>$data['courseName'],'cour_url'=>$data['courseLink'],'cour_language'=>$data['courseLan'],'cour_summary'=>$data['courSummary'],'cour_type'=>$type);
		}
		else if($type=='4')
		{
			$course = array('cour_name'=>$data['webName'],'cour_url'=>$data['webLink'],'cour_summary'=>$data['webSummary'],'cour_type'=>$type);		
		}
		else if($type=='2')
		{
			$course = array('cour_name'=>$data['bookName'],'cour_author'=>$data['bookAuthor'],'cour_pageamount'=>$data['bookPage'],'cour_publish'=>$data['bookPublish'],'cour_summary'=>$data['bookSummary'],'cour_type'=>$type);					
		}
		else if($type=='5')
		{
			$course = array('cour_summary'=>$data['articleSummary']);					
		}
		
		$course['submit_time'] =time();
		$course['user_id'] = $this->user_id;
		$course['cour_atl_id']=$atl_id;

		if($this->db->insert("zy_atlas_courses",$course))
		{
			return TRUE;
		}

	}
    
    /**
     * 查询图谱信息列表
     * 
     * @param 
     * 
     * @return 
     */
    public function selectAtlasLists()
    {
    	$query = "SELECT a.`atl_id`,a.`user_id`,a.`atl_name`,a.`atl_img`,u.`user_nickname`,u.`user_summary`,u.`head_pic`,u.`user_summary`,c.`cate_id`,c.`cate_name`
    	 FROM {$this->table_name} as a,`zy_user` as u,`zy_category` as c WHERE a.`user_id`=u.`user_id` AND c.`cate_id`=a.`atl_category`";
    	return $this->db->query($query)->result();
    }
    
    public function selectAtlasById($atl_id)
    {
    	$query ="SELECT * FROM `{$this->table_name}` as a,`zy_user` as u
    	WHERE a.`user_id` = u.`user_id` AND a.`atl_id`='{$atl_id}'";
    	return $this->db->query($query)->result();
    }
    
    /**
     * 检索altas的导航栏
     */
    public function selectAtlasNavbar()
    {
    	return $this->db->get('zy_atlas_navbar')->result();
    }
    
    /**
     * 检索所有的图谱课程的子内容
     */
    public function selectAtlCourse($cour_id , $type)
    {
    	if($type == 1)
		{
			$query = "SELECT ac.*,p.`pub_name`,p.`pub_id`,
    	u.`user_nickname`,u.`user_id`,u.`user_summary`
    	FROM `zy_user` as u,`zy_atlas_courses` AS ac LEFT JOIN `zy_publish` as p ON ac.`cour_publish`=p.`pub_id` 
    	WHERE ac.`cour_atl_id`='{$cour_id}'
    	AND ac.`user_id`=u.`user_id` ORDER BY `submit_time` DESC";
		}
		else
		{
			$query = "SELECT ac.*,p.`pub_name`,p.`pub_id`,
    	u.`user_nickname`,u.`user_id`,u.`user_summary`
    	FROM `zy_user` as u,`zy_atlas_courses` AS ac LEFT JOIN `zy_publish` as p ON ac.`cour_publish`=p.`pub_id` 
    	WHERE ac.`cour_atl_id`='{$cour_id}'
    	AND ac.`user_id`=u.`user_id` AND ac.`cour_type`='{$type}' ORDER BY `submit_time` DESC";
		}
    	
    	return $this->db->query($query)->result();
    }
    
    /**
	 * 查询关注该图谱的粉丝列表
	 */
	public function selectAtlFans($atl_id)
	{
		$query = "SELECT u.`user_id`,u.`head_pic`,u.`user_nickname` FROM `zy_atlas_fans` as af,`zy_user` as u WHERE af.`fan_atl_id`='{$atl_id}' AND af.`user_id`=u.`user_id`";
		return $this->db->query($query)->result();
	}
	
	/**
	 * 根据用户ID检索粉丝
	 */
	public function selectFansByUserId($user_id)
	{
		return $this->db->get_where('zy_atlas_fans',array('user_id'=>$user_id))->result();
	}
	
	/**
	 * 更新图谱的粉丝数
	 */
	public function updateFansAmount($atl_id,$type=false)
	{
		if($type=="1")
		{
			//加一个
			$query = "UPDATE `{$this->table_name}` SET atl_fans=atl_fans+1 WHERE `atl_id`='{$atl_id}'";
			if($this->db->query($query))
			{
				return TRUE;
			}
		}
		else
		{
			//减一个
			$query = "UPDATE `{$this->table_name}` SET atl_fans=atl_fans-1 WHERE `atl_id`='{$atl_id}'";
			if($this->db->query($query))
			{
				return TRUE;
			}
		}
	}
	
	/**
	 * 新增用户关注图谱
	 */
	public function addUserFocus($fan_atl_id)
	{
		$data = array('user_id'=>$this->user_id,'fan_time'=>time(),'fan_atl_id'=>$fan_atl_id);
		if( $this->db->insert("zy_atlas_fans",$data ) )
		{
			return true;
		}
	}
	
	/**
	 * 取消用户关注
	 */
	public function delUserFocus($fan_atl_id)
	{
		if( $this->db->delete("zy_atlas_fans",array("user_id"=>$this->user_id,'fan_atl_id'=>$fan_atl_id)) )
		{
			return true;
		}
	}

}